// Copyright 2011, Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package v201101;

import com.google.api.adwords.lib.AdWordsService;
import com.google.api.adwords.lib.AdWordsServiceLogger;
import com.google.api.adwords.lib.AdWordsUser;
import com.google.api.adwords.v201101.cm.DateRange;
import com.google.api.adwords.v201101.cm.DownloadFormat;
import com.google.api.adwords.v201101.cm.Operator;
import com.google.api.adwords.v201101.cm.Predicate;
import com.google.api.adwords.v201101.cm.PredicateOperator;
import com.google.api.adwords.v201101.cm.ReportDefinition;
import com.google.api.adwords.v201101.cm.ReportDefinitionDateRangeType;
import com.google.api.adwords.v201101.cm.ReportDefinitionOperation;
import com.google.api.adwords.v201101.cm.ReportDefinitionReportType;
import com.google.api.adwords.v201101.cm.ReportDefinitionServiceInterface;
import com.google.api.adwords.v201101.cm.Selector;

/**
 * This example adds a keywords performance report. To get ad groups, run
 * GetAllAdGroups.java. To get report columns, run GetReportColumns.java.
 *
 * Tags: ReportDefinitionService.mutate
 *
 * @author api.arogal@gmail.com (Adam Rogal)
 */
public class AddKeywordsPerformanceReportDefinition {
  public static void main(String[] args) {
    try {
      // Log SOAP XML request and response.
      AdWordsServiceLogger.log();

      // Get AdWordsUser from "~/adwords.properties".
      AdWordsUser user = new AdWordsUser();

      // Get the ReportDefinitionService.
      ReportDefinitionServiceInterface reportDefinitionService =
          user.getService(AdWordsService.V201101.REPORT_DEFINITION_SERVICE);

      Long adGroupId = Long.parseLong("INSERT_AD_GROUP_ID_HERE");
      String startDate = "INSERT_START_DATE_HERE";
      String endDate = "INSERT_END_DATE_HERE";

      // Create ad group predicate.
      Predicate adGroupPredicate = new Predicate();
      adGroupPredicate.setField("AdGroupId");
      adGroupPredicate.setOperator(PredicateOperator.EQUALS);
      adGroupPredicate.setValues(new String[] {adGroupId.toString()});

      // Create selector.
      Selector selector = new Selector();
      selector.setFields(new String[] {"AdGroupId", "Id", "KeywordText",
          "KeywordMatchType", "Impressions", "Clicks", "Cost"});
      selector.setPredicates(new Predicate[] {adGroupPredicate});
      selector.setDateRange(new DateRange(startDate, endDate));

      // Create report definition.
      ReportDefinition reportDefinition = new ReportDefinition();
      reportDefinition.setReportName("Keywords performance report #" + System.currentTimeMillis());
      reportDefinition.setDateRangeType(ReportDefinitionDateRangeType.CUSTOM_DATE);
      reportDefinition.setReportType(ReportDefinitionReportType.KEYWORDS_PERFORMANCE_REPORT);
      reportDefinition.setDownloadFormat(DownloadFormat.XML);
      reportDefinition.setSelector(selector);

      // Create operations.
      ReportDefinitionOperation operation = new ReportDefinitionOperation();
      operation.setOperand(reportDefinition);
      operation.setOperator(Operator.ADD);

      ReportDefinitionOperation[] operations = new ReportDefinitionOperation[] {operation};

      // Add report definition.
      ReportDefinition[] result = reportDefinitionService.mutate(operations);

      // Display report definitions.
      if (result != null) {
        for (ReportDefinition reportDefinitionResult : result) {
          System.out.println("Report definition with name \""
              + reportDefinitionResult.getReportName() + "\" and id \""
              + reportDefinitionResult.getId() + "\" was added.");
        }
      } else {
        System.out.println("No report definitions were added.");
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}
